package com.ssy.invoicing.mail;

import java.io.File;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.ssy.invoicing.mail.entity.MailMessage;

@Service(value="mailSendService")
public class MailSendService {

    @Resource
    private JavaMailSender  mailSender;
    
    @Resource
    private VelocityEngine velocityEngine;
    
    /**
     * 发送邮件,带有模版和附件
     * @author wb_suoyao.shen 2012.05.24
     * @param mailMessage
     */
    public void sendEmail(MailMessage mailMessage) {
        final MailMessage mail = mailMessage;
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            //注意MimeMessagePreparator接口只有这一个回调函数
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                //这是一个生成Mime邮件简单工具，如果不使用GBK这个，中文会出现乱码
                //如果您使用的都是英文，那么可以使用MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setTo(mail.getSendAddress());//设置接收方的email地址
                message.setSubject(mail.getSubject());//设置邮件主题
                message.setFrom(mail.getSendFrom());//设置发送方地址
                String context = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mail.getTemplateFile(),
                        "UTF-8", mail.getModel());
                //从模板中加载要发送的内容，vmfile就是模板文件的名字
                //注意模板中有中文要加GBK，model中存放的是要替换模板中字段的值
                message.setText(context, true);
                //将发送的内容赋值给MimeMessageHelper,后面的true表示内容解析成html
                //如果您不想解析文本内容，可以使用false或者不添加这项  
                if (mail.getFiles() != null && mail.getFiles().length > 0) {
                    FileSystemResource file;
                    for (String s : mail.getFiles())//添加附件
                    {
                        file = new FileSystemResource(new File(s));//读取附件
                        message.addAttachment(s, file);//向email中添加附件
                    }
                }
            }
        };
        mailSender.send(preparator);//发送邮件
    }
    
    /**
     * 发送简单的邮件
     * @author wb_suoyao.shen 2012.05.24
     * @param mailMessage
     */
    public void sendSimpleMail(MailMessage mailMessage) {
        final MailMessage mail = mailMessage;
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                message.setTo(mail.getSendAddress());
                message.setSubject(mail.getSubject());
                message.setFrom(mail.getSendFrom());
                message.setText(mail.getMailContext(), true);
            }
        };
        mailSender.send(preparator);//发送邮件
    }
}
